package com.dynamo.transport.network.practise.channel.socket.nio;

import java.util.concurrent.Executor;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author duanyimiao
 * @create 2018-12-17 11:49 AM
 * @description
 **/
public class NioBossPool {

    private Executor executor;
    private int bossCount;
    private Boss[] bosses;
    private AtomicInteger counter = new AtomicInteger();

    //TODO 对初始化的参数进行校验
    public NioBossPool(Executor executor, int bossCount) {
        this.executor = executor;
        this.bossCount = bossCount;
        bosses = new Boss[bossCount];
        initBoss(executor, bossCount);
    }

    private void initBoss(Executor executor, int workerCount) {

        for (int i = 0; i < workerCount; i++){
            bosses[i] = new NioBoss();
            executor.execute((NioBoss)bosses[i]);
        }
    }

    public Boss nextBoss(){
        return bosses[counter.getAndIncrement()%bossCount];
    }


}
